package com.tinyseed.emcp.api.exception.service;


import com.tinyseed.emcp.api.app.common.ListUtil;
import com.tinyseed.emcp.api.basicdata.service.OrganizeService;
import com.tinyseed.emcp.api.exception.bean.ExceptionStatisticsInputDTO;
import com.tinyseed.emcp.api.exception.bean.ExceptionStatisticsOutputDTO;
import com.tinyseed.emcp.api.exception.entity.ExceptionStatistics;
import com.tinyseed.emcp.api.exception.repository.ExceptionStatisticsRepository;
import com.tinyseed.hmxx.common.utils.QueryHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Clare
 */
@Service
@Slf4j
public class ExceptionStatisticsServiceImpl implements ExceptionStatisticsService {

    @Autowired
    private ExceptionStatisticsRepository exceptionStatisticsRepository;

    @Autowired
    private OrganizeService organizeService;

    @Override
    public List<ExceptionStatisticsOutputDTO> getList(int size, int current) {
        Pageable pageable = PageRequest.of(current, size);
        /*查找企业*/
        Page<ExceptionStatistics> exceptionStatisticsList = exceptionStatisticsRepository.findAll(pageable);
        /*转换格式为outputdto*/
        List<ExceptionStatisticsOutputDTO> exceptionStatisticsOutputDTOList = new ArrayList<>();
        exceptionStatisticsList.getContent().forEach(temp -> {
            ExceptionStatisticsOutputDTO exceptionStatisticsOutputDTO1 = new ExceptionStatisticsOutputDTO();
            BeanUtils.copyProperties(temp, exceptionStatisticsOutputDTO1);
            exceptionStatisticsOutputDTOList.add(exceptionStatisticsOutputDTO1);
        });
        return exceptionStatisticsOutputDTOList;
    }

    @Override
    public Page getList(ExceptionStatisticsInputDTO inputDTO, int size, int current) {

        Pageable pageable = PageRequest.of(current - 1, size);
        Page<ExceptionStatistics> exceptionStatisticsPage = exceptionStatisticsRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, inputDTO, criteriaBuilder), pageable);
        Page<ExceptionStatisticsOutputDTO> exceptionStatisticsOutputDTOList
                = ListUtil.copyPage(exceptionStatisticsPage, ExceptionStatisticsOutputDTO.class);
        return exceptionStatisticsPage;
    }

    @Override
    public Integer delete(Long id) {
        exceptionStatisticsRepository.deleteById(id);
        return 1;
    }


}
